package com.gzhryc.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.gzhryc.servlet.AbsVelocityTools;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.system.dao.db.Manager;

public class PowerVelocityTools extends AbsVelocityTools{
	
	public final static String Alias = "power";
	
	Manager manager;
	
	@Override
	public String getAlias() {
		return Alias;
	}
	
	@Override
	public void setRequest(HttpServletRequest request) {
		HttpSession session = request.getSession();
		manager = WebUtils.getAttribute(session,SysConstants.SESSION_LOGIN_MANAGER,Manager.class);
	}

	
	public boolean has(String... permission) {
		if(manager != null) {
			if(MenuService.self().hasByManagerIdAndPermission(manager.getId(),permission)) {
				return true;
			}
		}
		return false;
	}
	
	public boolean notHas(String... permission) {
		if(manager != null) {
			if(MenuService.self().hasByManagerIdAndPermission(manager.getId(),permission)) {
				return false;
			}
		}
		return true;
	}
	
	public Object hasTo(Object value,Object value1,String... permission) {
		if(manager != null) {
			if(MenuService.self().hasByManagerIdAndPermission(manager.getId(),permission)) {
				return value;
			}
		}
		return value1;
	}
	
	public Object notHasTo(Object value,Object value1,String... permission) {
		if(manager != null) {
			if(MenuService.self().hasByManagerIdAndPermission(manager.getId(),permission)) {
				return value;
			}
		}
		return value1;
	}
	
	public String display(String permission) {
		return display(permission,"block",true);
	}
	
	public String displayLine(String permission) {
		return display(permission,"inline-block",true);
	}
	
	public String display(String permission,String display,boolean hasStyle) {
		String result = "display:none;";
		if(has(permission)) {
			result = "display:"+display+";";
		}
		if(hasStyle) {
			result = "style=\""+result+"\"";
		}
		return result;
	}

}
